package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.UpdateStatusRequest;
import com.ruoyi.system.mapper.TBaseStorageAreaLocationMapper;
import org.apache.ibatis.annotations.Update;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TOngoodsshelf;
import com.ruoyi.system.service.ITOngoodsshelfService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2024-09-07
 */
@RestController
@RequestMapping("/system/ONGOODSSHELF")
public class TOngoodsshelfController extends BaseController
{
    @Autowired
    private ITOngoodsshelfService tOngoodsshelfService;

    @Autowired
    private TBaseStorageAreaLocationMapper tBaseStorageAreaLocationMapper;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:ONGOODSSHELF:list')")
    @GetMapping("/list")
    public TableDataInfo list(TOngoodsshelf tOngoodsshelf)
    {
        startPage();
        List<TOngoodsshelf> list = tOngoodsshelfService.selectTOngoodsshelfList(tOngoodsshelf);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:ONGOODSSHELF:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TOngoodsshelf tOngoodsshelf)
    {
        List<TOngoodsshelf> list = tOngoodsshelfService.selectTOngoodsshelfList(tOngoodsshelf);
        ExcelUtil<TOngoodsshelf> util = new ExcelUtil<TOngoodsshelf>(TOngoodsshelf.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:ONGOODSSHELF:query')")
    @GetMapping(value = "/{lotId}")
    public AjaxResult getInfo(@PathVariable("lotId") String lotId)
    {
        return success(tOngoodsshelfService.selectTOngoodsshelfByLotId(lotId));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:ONGOODSSHELF:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TOngoodsshelf tOngoodsshelf)
    {
        return toAjax(tOngoodsshelfService.insertTOngoodsshelf(tOngoodsshelf));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:ONGOODSSHELF:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TOngoodsshelf tOngoodsshelf)
    {
        return toAjax(tOngoodsshelfService.updateTOngoodsshelf(tOngoodsshelf));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:ONGOODSSHELF:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{lotIds}")
    public AjaxResult remove(@PathVariable String[] lotIds)
    {
        //根据库存状态改变库位状态
        return toAjax(tOngoodsshelfService.deleteTOngoodsshelfByLotIds(lotIds));
    }


    /**
     * 删除前修库位表状态
     */
//    @PreAuthorize("@ss.hasPermi('system:ONGOODSSHELF:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping("/status/{locationIds}")
    public AjaxResult update(@PathVariable String[] locationIds)
    {

        return toAjax(tOngoodsshelfService.selectSts(locationIds));
    }

    /**
     * 修改
     * @param request
     * @return
     */
    @PostMapping("/sts")
    public AjaxResult updateStatus(@RequestBody UpdateStatusRequest request) {


        List<String> locationId = request.getLocationId();

        List<TOngoodsshelf> lists = new ArrayList<>();


        for (String s : locationId) {
             TOngoodsshelf tOngoodsshelves = tOngoodsshelfService.locl(s);
             if (tOngoodsshelves.getStatus().equals("2")){
                 tOngoodsshelves.setStatus(request.getStatus());
                 Random random = new Random();
                 int randomNumber = 100 + random.nextInt(999); // 生成 100 到 999 之间的随机数
                 tOngoodsshelves.setPlcID(Long.valueOf(String.valueOf(randomNumber))); // 将随机数转换为字符串并设置
                 lists.add(tOngoodsshelves);
             }else {
                 tOngoodsshelves.setStatus(request.getStatus());
                 lists.add(tOngoodsshelves);
             }
        }


        return toAjax(tOngoodsshelfService.updateStatus(lists));
    }
}
